package com.ellecity06.common.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.viewpager.widget.ViewPager

/**
 * @author zavier
 * @time 2024/10/22 11:53
 * @des 自定义的viewpager
 */

class CustomViewPager @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null) : ViewPager(context, attributeSet) {
    private var isCanScroll = true
    private var onMyTouchListener: OnMyTouchListener? = null


    fun setOnMyTouchListener(onMyTouchListener: OnMyTouchListener?) {
        this.onMyTouchListener = onMyTouchListener
    }

    /**
     * 设置其是否能滑动换页
     * @param isCanScroll false 不能换页， true 可以滑动换页
     */
    fun setCanScroll(isCanScroll: Boolean) {
        this.isCanScroll = isCanScroll
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (onMyTouchListener != null) {
            onMyTouchListener!!.onTouch(this, ev)
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        return isCanScroll && super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        return isCanScroll && super.onTouchEvent(ev)
    }

    interface OnMyTouchListener {
        fun onTouch(view: View?, event: MotionEvent?): Boolean
    }
}